package com.lizhenhua.fmmall.service.serviceImpl;

import com.lizhenhua.fmmall.entity.ProductComments;
import com.lizhenhua.fmmall.entity.ProductCommentsVo;
import com.lizhenhua.fmmall.entity.ProductParams;
import com.lizhenhua.fmmall.mapper.ProductCommentsMapper;
import com.lizhenhua.fmmall.service.ProductCommentsService;
import com.lizhenhua.fmmall.utils.PageHelper;
import com.lizhenhua.fmmall.vo.ResStatus;
import com.lizhenhua.fmmall.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;

@Service
public class ProductCommentsVoListImpl implements ProductCommentsService {
    @Autowired
    private ProductCommentsMapper productCommentsMapper;


    @Override
    public ResultVo selectPageHelperCommentsByProduct(String productId, int pageNum,int limit) {

//        查询所有评论
//        List<ProductCommentsVo> productComments = productCommentsMapper.selectCommentsByProduct(productId);
//        分页查询
//        根据商品id查询总记录数
        Example example = new Example(ProductComments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId", productId);
        List<ProductComments> productComments = productCommentsMapper.selectByExample(example);
        // 总数据数
        int count = productCommentsMapper.selectCountByExample(example);
        // 计算总页数 （来确定每页显示多少条pageSize）由前端来确定每页显示多少条
        // 三目运算符
        int pageCount = count % limit == 0 ? count / limit : count / limit + 1;

        // 查询当前页的数据 （因为数据中包含用户信息，因此需要链表查询）

        // 计算从哪开始
        int start = (pageNum-1) * limit;

        List<ProductCommentsVo> productCommentsVosList = productCommentsMapper.selectPageHelperCommentsByProduct(productId, start, limit);


        if (productComments.size() < 1) {
            return new ResultVo(ResStatus.NO, "查询评论出错", null);
        } else {
            return new ResultVo(ResStatus.OK, "success", new PageHelper<ProductCommentsVo>(count, pageCount, productCommentsVosList));
        }
    }

    @Override
    public ResultVo getCountByProductId(String productId) {

        // 查询总记录数
        Example example = new Example(ProductComments.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId",productId);
       double counts = Double.valueOf(productCommentsMapper.selectCountByExample(example)) ;


        // 统计 1 好评记录数
        Example example1 = new Example(ProductComments.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("productId",productId);

        criteria1.andEqualTo("commType",1);
        double good =Double.valueOf(productCommentsMapper.selectCountByExample(example1));


        // 统计 0 中评
        Example example2 = new Example(ProductComments.class);
        Example.Criteria criteria2 = example2.createCriteria();
        criteria2.andEqualTo("productId",productId);
        criteria2.andEqualTo("commType","0");
        double mid = Double.valueOf(productCommentsMapper.selectCountByExample(example2));

        // 统计-1 差评
        Example example3 = new Example(ProductComments.class);
        Example.Criteria criteria3 = example3.createCriteria();
        criteria3.andEqualTo("productId",productId);
        criteria3.andEqualTo("commType","-1");

        double bad =  Double.valueOf(productCommentsMapper.selectCountByExample(example3));
// 保留两位小时




        // 计算好评lv
        String percentGood=String.format("%.2f",good /counts*100);
        String percentMid= String.format("%.2f",mid / counts*100);
        String percentBad= String.format("%.2f",bad /counts*100);

        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("counts",counts);
        hashMap.put("mid",mid);
        hashMap.put("good",good);
        hashMap.put("bad",bad);
        hashMap.put("percentGood",percentGood);
        hashMap.put("percentMid",percentMid);
        hashMap.put("percentBad",percentBad);

        return new ResultVo(ResStatus.OK,"success",hashMap);
    }

    @Override
    public ResultVo productCommentsVoList(String productId) {
        //        查询所有评论
        List<ProductCommentsVo> productComments = productCommentsMapper.selectCommentsByProduct(productId);


        if (productComments.size() < 1) {
            return new ResultVo(ResStatus.NO, "查询评论出错", null);
        } else {
            return new ResultVo(ResStatus.OK, "success", productComments);
        }
    }

}
